<template>
  <div>
    <el-card shadow="never">
      <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px">
        <div>{{ tableTitle }}</div>
        <div>
          <el-button type="primary" plain :icon="Plus" @click="getAdd">新增</el-button>
          <el-button type="primary" plain :icon="RefreshLeft" @click="getRefresh">刷新</el-button>
          <el-popover placement="bottom" trigger="click" width="auto">
            <template #reference>
              <el-button type="primary" plain :icon="Setting">列设置</el-button>
            </template>
            <el-checkbox-group v-model="colOptions" ref="sortableRef">
              <div v-for="(item, index) in column" :key="index" class="sortable-item">
                <el-icon style="font-size: 16px; margin-right: 6px"><Grid /></el-icon>
                <div style="flex: 1">
                  <el-checkbox :label="item.label" :value="item.label" class="checkboxItem"></el-checkbox>
                </div>
              </div>
            </el-checkbox-group>
          </el-popover>
        </div>
      </div>
      <el-table :data="tableData" style="width: 100%" border :max-height="tableHeight" :row-key="rowKey">
        <el-table-column
          v-for="item in tableColumn"
          :prop="item.field"
          :label="item.label"
          :width="item.width ? item.width : null"
          :fixed="item.fixed ? item.fixed : null"
          :align="item.align || 'center'">
          <template #default="scoped">
            <slot :name="item.slotName" :scope="scoped" :item="item" />
          </template>
        </el-table-column>
      </el-table>
      <template #footer v-if="isShowPaging">
        <el-pagination
          v-model:current-page="formParams.pageNo"
          v-model:page-size="formParams.pageSize"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="getPaging"
          @current-change="getPaging" />
      </template>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
  import Sortable from 'sortablejs'
  import { ref, onMounted, nextTick, watch, toRefs } from 'vue'
  import { Grid, Plus, Setting, RefreshLeft } from '@element-plus/icons-vue'
  const emits = defineEmits(['getAddorEdit', 'getRefresh', 'setPaging'])
  const props = defineProps({
    // 表格数据
    tableData: {
      type: Array,
      required: true,
      default: [],
    },
    // 表格表头数据
    column: {
      type: Array,
      required: true,
      default: [],
    },
    // 数据总条数
    total: {
      type: Number,
      default: 0,
    },
    // 表格高度
    tableHeight: {
      type: Number,
      default: 465,
    },
    // 是否显示分页
    isShowPaging: {
      type: Boolean,
      default: true,
    },
    // 表格标题
    tableTitle: {
      type: String,
      default: '',
    },
    // 分页参数
    formParams: {
      type: Object,
      default: {},
    },
    // 表格rowKey
    rowKey: {
      type: String,
      default: '',
    },
  })
  const { tableData, total, formParams, column, tableHeight, isShowPaging, tableTitle, rowKey }: any = toRefs(props)
  const colOptions = ref<string[]>([])
  const tableColumn = ref<any>(column.value)
  const sortableRef = ref<any>(null)
  const initSortable = () => {
    const wrapperTr = sortableRef.value?.$el
    new Sortable(wrapperTr, {
      delay: 10,
      animation: 150,
      draggable: '.sortable-item',
      onEnd: (evt: any) => {
        if (evt.oldIndex == evt.newIndex) return
        const oldItem = tableColumn.value[evt.oldIndex]
        const newItem = tableColumn.value[evt.newIndex]
        tableColumn.value[evt.oldIndex] = newItem
        tableColumn.value[evt.newIndex] = oldItem
      },
    })
  }
  colOptions.value = tableColumn.value.map((item: any) => item.label)
  watch(colOptions, (newVal) => {
    console.log(newVal)
    tableColumn.value = column.value.filter((i: any) => newVal.indexOf(i.label) != -1)
    console.log(tableColumn.value)
  })
  onMounted(() => {
    nextTick(() => {
      initSortable()
    })
  })
  const getAdd = () => {
    emits('getAddorEdit', {})
  }
  const getRefresh = () => {
    emits('getRefresh')
  }
  const getPaging = () => {
    emits('setPaging', formParams.value)
  }
</script>

<style lang="scss" scoped>
  .sortable-item {
    display: flex;
    align-items: center;
    .el-icon {
      &:hover {
        cursor: move;
      }
    }
    .checkboxItem {
      display: flex;
      align-items: center;
      width: 100%;
      &:hover {
        background: rgba(135, 207, 235, 0.201);
      }
    }
  }
  :deep(.el-card__body) {
    padding: 10px;
  }
</style>
